Introduce expand_string and a new module for logging (mostly for debugging)#40
Conversation
This commit contains a very helpful little subroutine that can help with
formatting strings. This subroutine was written by Matt Hoffman for the
NCAR's/Los Alamos' MPAS-Model.
It is slightly akin to a printf function:
```Fortran
call expand_string("my string int: $i real: $r", outString, intArgs=(/3/), realArgs=(/42.0/))
```
This function is not used anywhere in ModEM at the moment, but will be helpful
for a future commit that implements a new method for logging; however, this
subroutine should be helpful in various other places, thus I have placed it in
utilities.
This commit adds a new module, ModEM_logger, which contains an easier to use logging subroutine. There are several notable helpful features of this logger: 1. ModEM_log uses expand_string which was introduced in the last commit, easily allowing logs to write out specific arguments rather than formatting their own. 2. Using the `ModEM_log_init` subroutine, each individual task can open its own unique logging file. This can be very helpful when debugging MPI issues. I have taken the log subroutines from MPAS-Model, and thus included it's license. I have modified ModEM_log to also take a file unit, thus, instead of calling ModEM_log_init, one can just call ModEM_log on a file unit already used by ModEM (say, 0 or 6). Currently, this module is not used, and I don't have plans to replace any of ModEM's log statements with it, but I keep added it on various branches for testing, and thus figured perhaps I should open a pull request to include it into main.
dong-hao
left a comment
There was a problem hiding this comment.
Thanks @MiCurry - looks like a great new feature. Indeed, that would be nice to have for MPI diagnostics - although implementation throughout the repo could involve some extensive works. Do you happen to have a working example to showcase how it works in practice?
|
@dong-hao I've added some examples to the code comments for the ModEM_log subroutine, but here they are as well: real :: a, b, c ! Of course you can also use other real kinds
integer :: int1, int2,
logical :: bool_false, bool_true
! Ensure you call ModEM_log_init() first!
call ModEM_log_init()
! Printing several real numbers:
call ModEM_log("Reals: a: $r b: $r c: $r", realArgs=(/a, b, c/)))
! Printing several integer numbers
call ModEM_log("Integers: ($i, $i)", intArgs=(/int1, int2/)))
! Printing logicals
bool_false = .false.
bool_true = .true.
call ModEM_log("Logical: $l $l", logicArgs=(/bool_false, bool_true/))
! Mix and match as you see fit!
! Note: You do not need to have the arguments (intArgs, logicArgs etc.) in any particular order!
call ModEM_log("Bool: $l - Int: $i - Reals: $r $r $r", intArgs=(/int1/), logicArgs=(/bool_true/), &
realArgs=(/a, b, c/))
! Appending Strings
myString = "JOB_NAME"
call ModEM_log("We are in the: '"//trim(myString)//"' function with args: $i, $i", intArgs=(/int1, int2/))I've been using it quite extensively for the last few weeks, it has some interface quirks that I'd like to work out before including it, but it is very helpful! |
There was a problem hiding this comment.
Thanks @MiCurry for introducing the log utilities。 I drafted a simple unit test code according to your example - I can confirm that the part is running fine as expected. Indeed it is a nice method to have when debugging the parallel code as it provides separate log per task. This is especially useful when one has hundreds of processes to keep track of... The only small issue I found is that the debug remnant (just needs to be commented out).
|
Thanks for giving it a try out @dong-hao. Please feel free to give it another review now that I have addressed your changes. |
This pull requests adds in a new utility subroutine,
expand_stringand a new module,ModEM_logger.Both of the
expand_stringsubroutine and theModEM_loggersubroutine are taken from NCAR/Los Alamos National Labratory's MPAS-Model: https://github.com/MPAS-Dev/MPAS-Model/blob/master/src/framework/mpas_log.F. MPAS has a BSD licensces and I have included the license in the source code whereexpand_stringand modifiedMPAS_log_writereside.Currently, either
expand_stringor theModEM_loggerare not used anywhere in ModEM (outside ofModEM_loggerusingexpand_string). Theexpand_stringis a very helpful little subroutine that allows a 'printf'-like behavior to formatting strings. There should be numerous usages for such a method.The ModEM_log is also a helpful function in that it: uses
expand_stringand individual tasks can open their own logs for writing. This can help wonderfully when debugging MPI issues.While eventually we could replace all previous write log statements, for now I'd like to include these subroutines for debugging and convenience.